//
//  StatisticView.m
//  LClass
//
//  Created by 严军 on 16/6/4.
//  Copyright © 2016年 YanJun. All rights reserved.
//

#import "StatisticView.h"
#import <QuartzCore/QuartzCore.h>
@implementation StatisticView
{
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(id)initWithDataDic:(NSDictionary*)Dic
{
    self=[super init];
    if (self) {
        _dataDic=Dic;
        self.backgroundColor=[UIColor whiteColor];
        [self layOut];
    }
    return self;
}

-(void)layOut
{
    //设置滑动视图
    UIScrollView* scrollView=[[UIScrollView alloc]init];
    [self addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
        make.width.equalTo(self.mas_width);
    }];
    //[scrollView setBackgroundColor:[UIColor whiteColor]];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];
    [scrollView setBackgroundColor:ThemeBackgroundColor];
    //设置内容视图
    UIView* contentView=[[UIView alloc]init];
    [scrollView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
    }];
    [contentView setBackgroundColor:[UIColor whiteColor]];
    //设置标题1
    UIImageView* imgVTitle1=[[UIImageView alloc]init];
    [contentView addSubview:imgVTitle1];
    [imgVTitle1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(10);
        make.width.height.mas_equalTo(20);
    }];
    [imgVTitle1 setImage:[UIImage imageNamed:@"title_icon"]];
    
    UILabel* labTitle=[[UILabel alloc]init];
    [contentView addSubview:labTitle];
    [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgVTitle1.mas_right).offset(10);
        make.centerY.equalTo(imgVTitle1.mas_centerY);
        
    }];
    labTitle.font=[UIFont boldSystemFontOfSize:14];
    labTitle.textColor=[UIColor darkGrayColor];
    labTitle.text=@"本次考勤记录";
    
    //画线
    UIView* dashLine1=[[UIView alloc]init];
    [contentView addSubview:dashLine1];
    [dashLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imgVTitle1.mas_bottom).offset(8);
        make.width.equalTo(contentView).multipliedBy(0.9);
        make.centerX.equalTo(contentView);
        make.height.mas_equalTo(1);
    }];
    dashLine1.backgroundColor=[UIColor colorWithWhite:0.9 alpha:0.5];
    
    //饼图
    _chart=[[VBPieChart alloc]initWithFrame:CGRectMake(0, 0, 150*SizeScal, 150*SizeScal)];
    _chart.center=CGPointMake(ScreenWidth/2, 150*SizeScal/2+50);
    [contentView addSubview:_chart];
    [_chart setHoleRadiusPrecent:0.2];
    [_chart setLabelsPosition:VBLabelsPositionOnChart];
    int all = ((NSNumber*)[_dataDic objectForKey:@"allStuNum"]).intValue;
    int absent = ((NSNumber*)[_dataDic objectForKey:@"absentNum"]).intValue;
    int vacate = ((NSNumber*)[_dataDic objectForKey:@"vacate"]).intValue;
    int present=all-absent-vacate;
    
    [_chart setChartValues:@[
                             @{@"name":@"到课率", @"value":[NSString stringWithFormat:@"%d",present], @"color":TitleColorLight},
                             @{@"name":@"缺课率", @"value":[NSString stringWithFormat:@"%d",absent], @"color":[UIColor redColor]},
                             @{@"name":@"请假率", @"value":[NSString stringWithFormat:@"%d",vacate], @"color":[UIColor purpleColor]}
                             ]
                 animation:YES
                  duration:0.5
                   options:VBPieChartAnimationDefault];
    //本次考勤人数
    UILabel* labCurrent=[[UILabel alloc]init];
    [contentView addSubview:labCurrent];
    [labCurrent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_chart.mas_bottom).offset(20);
        make.left.equalTo(contentView).offset(20);
    }];
    labCurrent.font=[UIFont systemFontOfSize:18];
    labCurrent.textColor=[UIColor grayColor];
    labCurrent.text=@"本次考勤人数:";
    
    UILabel* labCurrentNum=[[UILabel alloc]init];
    [contentView addSubview:labCurrentNum];
    [labCurrentNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(labCurrent.mas_right).offset(10);
        make.centerY.equalTo(labCurrent.mas_centerY);
    }];
    labCurrentNum.font=[UIFont boldSystemFontOfSize:20];
    labCurrentNum.textColor=TitleColorLight;
    labCurrentNum.text=[_dataDic objectForKey:@"allStuNum"];

    //缺课人次
    UILabel* labAbsentNum=[[UILabel alloc]init];
    [contentView addSubview:labAbsentNum];
    [labAbsentNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(contentView.mas_right).offset(-20);
        make.centerY.equalTo(labCurrent.mas_centerY);
    }];
    labAbsentNum.font=[UIFont boldSystemFontOfSize:20];
    labAbsentNum.textColor=[UIColor redColor];
    labAbsentNum.text=[NSString stringWithFormat:@"%d",absent+vacate];
    
    UILabel* labAbsent=[[UILabel alloc]init];
    [contentView addSubview:labAbsent];
    [labAbsent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(labAbsentNum.mas_left).offset(-10);
        make.centerY.equalTo(labAbsentNum);
    }];
    labAbsent.font=[UIFont systemFontOfSize:18];
    labAbsent.textColor=[UIColor grayColor];
    labAbsent.text=@"缺课人次:";

    //到课率：
    UILabel* labAttend=[[UILabel alloc]init];
    [contentView addSubview:labAttend];
    [labAttend mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labCurrent.mas_bottom).offset(30);
        make.left.equalTo(labCurrent.mas_left);
    }];
    labAttend.font=[UIFont systemFontOfSize:18];
    labAttend.textColor=[UIColor grayColor];
    labAttend.text=@"到课率:";
    
    UILabel* labAttendNum=[[UILabel alloc]init];
    [contentView addSubview:labAttendNum];
    [labAttendNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(labAttend.mas_right).offset(10);
        make.centerY.equalTo(labAttend.mas_centerY);
    }];
    labAttendNum.font=[UIFont boldSystemFontOfSize:20];
    labAttendNum.textColor=TitleColorLight;
    float presentRat=((float)present/(float)all)*100;
    labAttendNum.text=[NSString stringWithFormat:@"%.1f%%",presentRat];

    //缺课率
    UILabel* labAbsentRate=[[UILabel alloc]init];
    [contentView addSubview:labAbsentRate];
    [labAbsentRate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(labAttend.mas_centerY);
        make.right.equalTo(contentView.mas_right).offset(-20);
    }];
    labAbsentRate.font=[UIFont boldSystemFontOfSize:20];
    labAbsentRate.textColor=[UIColor redColor];
    labAbsentRate.text=[NSString stringWithFormat:@"%.1f%%",100-presentRat];
    
    UILabel* labAbsentR=[[UILabel alloc]init];
    [contentView addSubview:labAbsentR];
    [labAbsentR mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(labAbsentRate.mas_left).offset(-10);
        make.centerY.equalTo(labAbsentRate);
    }];
    labAbsentR.font=[UIFont systemFontOfSize:18];
    labAbsentR.textColor=[UIColor grayColor];
    labAbsentR.text=@"缺课率:";
    
    //所有考勤
    //设置标题1
    UIImageView* imgVTitle2=[[UIImageView alloc]init];
    [contentView addSubview:imgVTitle2];
    [imgVTitle2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labAttend.mas_bottom).offset(30);
        make.left.equalTo(contentView).offset(10);
        make.width.height.mas_equalTo(20);
    }];
    [imgVTitle2 setImage:[UIImage imageNamed:@"title_icon"]];
    
    UILabel* labTitle2=[[UILabel alloc]init];
    [contentView addSubview:labTitle2];
    [labTitle2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgVTitle2.mas_right).offset(10);
        make.centerY.equalTo(imgVTitle2.mas_centerY);
        
        
    }];
    labTitle2.font=[UIFont boldSystemFontOfSize:14];
    labTitle2.textColor=[UIColor darkGrayColor];
    labTitle2.text=@"所有考勤记录";

    //画线
    UIView* dashLine2=[[UIView alloc]init];
    [contentView addSubview:dashLine2];
    [dashLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imgVTitle2.mas_bottom).offset(8);
        make.width.equalTo(contentView).multipliedBy(0.9);
        make.centerX.equalTo(contentView);
        make.height.mas_equalTo(1);
    }];
    dashLine2.backgroundColor=[UIColor colorWithWhite:0.9 alpha:0.5];

    //所有烤漆lab
    UILabel* lastLab;
    for (int i=0; i<3; i++) {
        
        //标题
        UILabel* lab=[[UILabel alloc]init];
        [contentView addSubview:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i==0) {
                make.top.equalTo(dashLine2.mas_bottom).offset(20);
            }else
            {
                make.top.equalTo(lastLab.mas_bottom).offset(20);
            }
            make.left.equalTo(contentView.mas_left).offset(20);
        }];
        lab.font=[UIFont systemFontOfSize:14];
        lab.textColor=[UIColor lightGrayColor];
        
        //数字
        UILabel* labNum=[[UILabel alloc]init];
        [contentView addSubview:labNum];
        [labNum mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lab.mas_right).offset(20);
            make.centerY.equalTo(lab.mas_centerY);
        }];
        labNum.font=[UIFont systemFontOfSize:14];
        labNum.textColor=TitleColorLight;
        
        //第二列数字
        UILabel* labNumSecond=[[UILabel alloc]init];
        [contentView addSubview:labNumSecond];
        [labNumSecond mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(contentView.mas_right).offset(-20);
            make.centerY.equalTo(lab.mas_centerY);
        }];
        labNumSecond.font=[UIFont systemFontOfSize:14];
        labNumSecond.textColor=TitleColorLight;
        
        //第二列标题
        UILabel* labSecond=[[UILabel alloc]init];
        [contentView addSubview:labSecond];
        [labSecond mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(labNumSecond.mas_left).offset(-20);
            make.centerY.equalTo(lab.mas_centerY);
        }];
        labSecond.font=[UIFont systemFontOfSize:14];
        labSecond.textColor=[UIColor lightGrayColor];
        
        switch (i) {
            case 0:
                lab.text=@"累计考勤次数:";
                labNum.text=[(NSNumber*)[_dataDic objectForKey:@"allCheckCount"] stringValue];
                labSecond.text=@"选课人数:";
                labNumSecond.text=[(NSNumber*)[_dataDic objectForKey:@"chooseCourseStuNum"] stringValue];
                break;
            case 1:
                lab.text=@"学生到课人次:";
                labNum.text=[(NSNumber*)[_dataDic objectForKey:@"allArriveNum"] stringValue];
                labSecond.text=@"到课率:";
                labNumSecond.text=[_dataDic objectForKey:@"allArriveRate"];
                break;
            case 2:
                lab.text=@"学生缺课人次:";
                labNum.text=[(NSNumber*)[_dataDic objectForKey:@"allAbsenceStuNums"] stringValue];
                labSecond.text=@"缺课率:";
                labNumSecond.text=[_dataDic objectForKey:@"allAbsenceRate"];
                labNumSecond.textColor=[UIColor redColor];
                break;
                
            default:
                break;
        }
         lastLab=lab;
    }
    UIButton* btnOK=[[UIButton alloc]init];
    [contentView addSubview:btnOK];
    [btnOK mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastLab.mas_bottom).offset(30);
        make.centerX.equalTo(contentView.mas_centerX);
        make.width.equalTo(contentView.mas_width).multipliedBy(0.7);
        make.height.mas_equalTo(40);
    }];
    [[Utility sharedInstance]setLayerView:btnOK borderW:1 borderColor:[UIColor lightGrayColor] radius:8];
    [btnOK setTitleColor:[UIColor darkGrayColor] forState:0];
    [btnOK setBackgroundColor:ThemeBackgroundColor];
    btnOK.titleLabel.font=[UIFont systemFontOfSize:16];
    [btnOK setTitle:@"确定" forState:0];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(btnOK.mas_bottom).offset(10);
    }];
}



@end
